package com.se.utils;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.regex.Pattern;

public class StringUtil {
	  public static int ONE_BYTE_MIN = 0;
	  public static int ONE_BYTE_MAX = 127;
	  public static int TWO_BYTE_MIN = 2048;
	  public static int TWO_BYTE_MAX = 2047;
	  public static int THREE_BYTE_MIN = 2048;
	  public static int THREE_BYTE_MAX = 65535;
	  public static int SURROGATE_MIN = 65536;
	  public static int SURROGATE_MAX = 1114111;

	  public static String alphaNumOnly(String paramString)
	  {
	    int i = paramString.length();
	    StringBuffer localStringBuffer = new StringBuffer(i);
	    for (int j = 0; j < i; j++)
	    {
	      char c = paramString.charAt(j);
	      if (((c < 'a') || (c > 'z')) && ((c < 'A') || (c > 'Z')) && ((c < '0') || (c > '9')) && (c != '_') && (c != '-') && (c != ' '))
	        continue;
	      localStringBuffer.append(c);
	    }
	    return localStringBuffer.toString();
	  }

	  public static boolean isAlphaNumOnly(String paramString)
	  {
	    int i = paramString.length();
	    for (int j = 0; j < i; j++)
	    {
	      int k = paramString.charAt(j);
	      if (((k < 97) || (k > 122)) && ((k < 65) || (k > 90)) && ((k < 48) || (k > 57)) && (k != 95) && (k != 45) && (k != 32))
	        return false;
	    }
	    return true;
	  }

	  public static boolean isAlpha(char paramChar)
	  {
	    return ((paramChar >= 'a') && (paramChar <= 'z')) || ((paramChar >= 'A') && (paramChar <= 'Z'));
	  }

	  public static boolean isNumeric(char paramChar)
	  {
	    return (paramChar >= '0') && (paramChar <= '9');
	  }

	  public static String normalizeWhitespace(String paramString)
	  {
	    StringBuffer localStringBuffer = new StringBuffer();
	    int i = paramString.length();
	    int j = 0;
	    for (int k = 0; k < i; k++)
	    {
	      char c = paramString.charAt(k);
	      switch (c)
	      {
	      case '\t':
	      case '\n':
	      case '\r':
	      case ' ':
	        if (j != 0)
	          continue;
	        localStringBuffer.append(' ');
	        j = 1;
	        break;
	      default:
	        localStringBuffer.append(c);
	        j = 0;
	      }
	    }
	    return localStringBuffer.toString();
	  }

	  public static int numOccurrences(String paramString, char paramChar)
	  {
	    int i = 0;
	    int j = 0;
	    int m = paramString.length();
	    while (j < m)
	    {
	      int k = paramString.indexOf(paramChar, j);
	      if (k < 0)
	        break;
	      i++;
	      j = k + 1;
	    }
	    return i;
	  }

	  public static String removeCharacters(String paramString1, String paramString2)
	  {
	    int i = paramString1.length();
	    StringBuffer localStringBuffer = new StringBuffer(i);
	    for (int j = 0; j < i; j++)
	    {
	      char c = paramString1.charAt(j);
	      if (paramString2.indexOf(c) != -1)
	        continue;
	      localStringBuffer.append(c);
	    }
	    return localStringBuffer.toString();
	  }

	  public static String removeWhiteSpace(String paramString)
	  {
	    int i = paramString.length();
	    StringBuffer localStringBuffer = new StringBuffer(i);
	    for (int j = 0; j < i; j++)
	    {
	      char c = paramString.charAt(j);
	      if (Character.isWhitespace(c))
	        continue;
	      localStringBuffer.append(c);
	    }
	    return localStringBuffer.toString();
	  }

	  public static String replace(String paramString, String[] paramArrayOfString1, String[] paramArrayOfString2)
	  {
	    if ((paramString == null) || (paramArrayOfString1 == null) || (paramArrayOfString2 == null))
	      return paramString;
	    for (int i = 0; i < paramArrayOfString1.length; i++)
	      paramString = replace(paramString, paramArrayOfString1[i], paramArrayOfString2[i]);
	    return paramString;
	  }

	  public static String replace(String paramString1, String paramString2, String paramString3)
	  {
	    if ((paramString1 == null) || (paramString2 == null) || (paramString3 == null))
	      return paramString1;
	    int i = paramString1.indexOf(paramString2);
	    if (i == -1)
	      return paramString1;
	    StringBuffer localStringBuffer = new StringBuffer(paramString1);
	    while (i != -1)
	    {
	      localStringBuffer.delete(i, i + paramString2.length());
	      localStringBuffer.insert(i, paramString3);
	      i = localStringBuffer.toString().indexOf(paramString2, i + paramString3.length());
	    }
	    return localStringBuffer.toString();
	  }

	  public static String[] splitStringAtCharacter(String paramString, char paramChar)
	  {
	    String[] arrayOfString = new String[numOccurrences(paramString, paramChar) + 1];
	    splitStringAtCharacter(paramString, paramChar, arrayOfString, 0);
	    return arrayOfString;
	  }

	  protected static int splitStringAtCharacter(String paramString, char paramChar, String[] paramArrayOfString, int paramInt)
	  {
	    int i = 0;
	    int j = paramInt;
	    int k = 0;
	    int n = paramString.length();
	    while ((k <= n) && (j < paramArrayOfString.length))
	    {
	      int m = paramString.indexOf(paramChar, k);
	      if (m < 0)
	        m = n;
	      paramArrayOfString[j] = paramString.substring(k, m);
	      i++;
	      j++;
	      k = m + 1;
	    }
	    return i;
	  }

	  public static boolean string2Boolean(String paramString)
	  {
	    if (paramString == null)
	      return false;
	    if (paramString.equalsIgnoreCase("true"))
	      return true;
	    if (paramString.equalsIgnoreCase("y"))
	      return true;
	    if (paramString.equalsIgnoreCase("yes"))
	      return true;
	    if (paramString.equalsIgnoreCase("ok"))
	      return true;
	    if (paramString.equalsIgnoreCase("okay"))
	      return true;
	    if (paramString.equalsIgnoreCase("on"))
	      return true;
	    return paramString.equalsIgnoreCase("1");
	  }

	  public static int string2Int(String paramString)
	  {
	    try
	    {
	      return Integer.parseInt(paramString);
	    }
	    catch (NumberFormatException localNumberFormatException)
	    {
	    }
	    return 0;
	  }

	  public static Map string2Hashtable(String paramString)
	  {
	    HashMap localHashMap = new HashMap();
	    paramString = normalizeWhitespace(paramString);
	    String[] arrayOfString = splitStringAtCharacter(paramString, ' ');
	    for (int i = 0; i < arrayOfString.length; i++)
	    {
	      int j = arrayOfString[i].indexOf('=');
	      String str1 = arrayOfString[i].substring(0, j);
	      String str2 = arrayOfString[i].substring(j + 1);
	      localHashMap.put(str1, str2);
	    }
	    return localHashMap;
	  }

	  public static String hashtable2String(Map paramMap)
	  {
	    Iterator localIterator = paramMap.keySet().iterator();
	    StringBuffer localStringBuffer = new StringBuffer();
	    while (localIterator.hasNext())
	    {
	      String str1 = "";
	      String str2 = "";
	      try
	      {
	        str1 = (String)localIterator.next();
	        str2 = (String)paramMap.get(str1);
	        localStringBuffer.append(str1);
	        localStringBuffer.append("=");
	        localStringBuffer.append(str2);
	        localStringBuffer.append(" ");
	      }
	      catch (ClassCastException localClassCastException)
	      {
	      }
	    }
	    return localStringBuffer.toString().trim();
	  }

	 
	  public static Vector tokenizer(String paramString1, String paramString2)
	  {
	    if (paramString1 == null)
	      return null;
	    Vector localVector = null;
	    StringTokenizer localStringTokenizer = new StringTokenizer(paramString1, paramString2);
	    while (localStringTokenizer.hasMoreTokens())
	    {
	      if (localVector == null)
	        localVector = new Vector();
	      localVector.addElement(localStringTokenizer.nextToken().trim());
	    }
	    return localVector;
	  }

	  public static String escapeHtmlString(String paramString)
	  {
	    String str = paramString;
	    if (str == null)
	      return null;
	    if (str.indexOf('&', 0) != -1)
	      str = replace(str, "&", "&amp;");
	    if (str.indexOf('<', 0) != -1)
	      str = replace(str, "<", "&lt;");
	    if (str.indexOf('>', 0) != -1)
	      str = replace(str, ">", "&gt;");
	    if (str.indexOf('"', 0) != -1)
	      str = replace(str, "\"", "&quot;");
	    if (str.indexOf('\'', 0) != -1)
	      str = replace(str, "'", "&#39;");
	    return str;
	  }

	  public static String reEscapeHtmlString(String paramString)
	  {
	    String str = paramString;
	    if (str == null)
	      return null;
	    String[] arrayOfString1 = { "&amp;", "&lt;", "&gt;", "&quot;", "&#39;" };
	    String[] arrayOfString2 = { "&", "<", ">", "\"", "'" };
	    return replace(str, arrayOfString1, arrayOfString2);
	  }

	  public static String fill(char paramChar, int paramInt)
	  {
	    if (paramInt <= 0)
	      return "";
	    char[] arrayOfChar = new char[paramInt];
	    for (int i = 0; i < paramInt; i++)
	      arrayOfChar[i] = paramChar;
	    return new String(arrayOfChar);
	  }

	  public static String padRight(String paramString, char paramChar, int paramInt)
	  {
	    return paramString + fill(paramChar, paramInt - paramString.length());
	  }

	  public static String padLeft(String paramString, char paramChar, int paramInt)
	  {
	    return fill(paramChar, paramInt - paramString.length()) + paramString;
	  }

	  public static String cutString(String paramString1, int paramInt, String paramString2)
	  {
	    if (paramString1 == null)
	      return paramString1;
	    if (paramInt <= 0)
	      return paramString1;
	    if ("UTF-8".equals(System.getProperty("file.encoding")))
	    {
	      if (paramString1.length() < paramInt)
	        return paramString1;
	      return paramString1.substring(0, paramInt) + paramString2;
	    }
	    byte[] arrayOfByte1 = paramString1.getBytes();
	    if (arrayOfByte1.length <= paramInt)
	      return paramString1;
	    char[] arrayOfChar = paramString1.toCharArray();
	    int i = paramInt;
	    for (int j = 0; j < arrayOfChar.length; j++)
	    {
	      if (arrayOfChar[j] < 'Ā')
	        i--;
	      else
	        i -= 2;
	      if (i <= 0)
	        break;
	    }
	    byte[] arrayOfByte2 = new byte[paramInt + i];
	    for (int k = 0; k < arrayOfByte2.length; k++)
	      arrayOfByte2[k] = arrayOfByte1[k];
	    return new String(arrayOfByte2) + paramString2;
	  }

	  public static String cutOffString(String paramString1, int paramInt, String paramString2, String paramString3)
	    throws UnsupportedEncodingException
	  {
	    if (paramString1 == null)
	      return null;
	    if (paramString1.length() == 0)
	      return paramString1;
	    byte[] arrayOfByte1 = paramString1.getBytes(paramString3);
	    if (arrayOfByte1.length <= paramInt)
	      return paramString1;
	    int i = 0;
	    int j = 0;
	    for (int k = 0; k < paramString1.length(); k++)
	    {
	      char c = paramString1.charAt(k);
	      j += availibleByteNum(c);
	      if (j <= paramInt)
	        continue;
	      i = j - availibleByteNum(c);
	      break;
	    }
	    byte[] arrayOfByte2 = new byte[i];
	    System.arraycopy(arrayOfByte1, 0, arrayOfByte2, 0, i);
	    String str = new String(arrayOfByte2, paramString3);
	    str = str + paramString2;
	    return str;
	  }

	  public static int availibleByteNum(char paramChar)
	  {
	    int i = paramChar;
	    if ((ONE_BYTE_MIN <= i) && (i <= ONE_BYTE_MAX))
	      return 1;
	    if ((TWO_BYTE_MIN <= i) && (i <= TWO_BYTE_MAX))
	      return 2;
	    if ((THREE_BYTE_MIN <= i) && (i <= THREE_BYTE_MAX))
	      return 3;
	    if ((SURROGATE_MIN <= i) && (i <= SURROGATE_MAX))
	      return 4;
	    return -1;
	  }

	  public static String toString(Object[] paramArrayOfObject)
	  {
	    return toString(paramArrayOfObject, ",");
	  }

	  public static String toString(Object[] paramArrayOfObject, String paramString)
	  {
	    if (paramArrayOfObject == null)
	      return null;
	    StringBuffer localStringBuffer = new StringBuffer();
	    for (int i = 0; i < paramArrayOfObject.length; i++)
	    {
	      if (i > 0)
	        localStringBuffer.append(paramString);
	      if (paramArrayOfObject[i] == null)
	        localStringBuffer.append("null");
	      else
	        localStringBuffer.append(paramArrayOfObject[i].toString());
	    }
	    return localStringBuffer.toString();
	  }

	  public static String toString(List paramList, String paramString)
	  {
	    StringBuffer localStringBuffer = new StringBuffer();
	    for (int i = 0; i < paramList.size(); i++)
	    {
	      if (i > 0)
	        localStringBuffer.append(paramString);
	      localStringBuffer.append(paramList.get(i).toString());
	    }
	    return localStringBuffer.toString();
	  }

	  public static String toConvert(String paramString1, String paramString2, String paramString3)
	    throws UnsupportedEncodingException
	  {
	    if (paramString1 == null)
	      return "";
	    return new String(paramString1.getBytes(paramString2), paramString3);
	  }

	  public static String NVL(String paramString)
	  {
	    if (paramString == null)
	      return "";
	    return paramString;
	  }

	  public static String NVL(String paramString1, String paramString2)
	  {
	    if ((paramString1 == null) || (paramString1.length() <= 0))
	      return paramString2;
	    return paramString1;
	  }

	  public static String byteMask(long paramLong)
	  {
	    double d = 0.0D;
	    d = paramLong / 1048576.0D;
	    String str1 = String.valueOf(d);
	    String str2 = str1.substring(str1.indexOf("."));
	    if (d > 1.0D)
	    {
	      if (str2.length() > 2)
	        str1 = str1.substring(0, str1.indexOf(".") + 3);
	      str1 = str1 + "M";
	    }
	    else
	    {
	      d = paramLong / 1024.0D;
	      str1 = String.valueOf(d);
	      str2 = str1.substring(str1.indexOf("."));
	      if (str2.length() > 2)
	        str1 = str1.substring(0, str1.indexOf(".") + 3);
	      str1 = str1 + "K";
	    }
	    return str1;
	  }

	  public static String removeTag(String paramString)
	  {
	    int i = 0;
	    int j = 0;
	    char[] arrayOfChar = paramString.toCharArray();
	    StringBuffer localStringBuffer = new StringBuffer();
	    for (int k = 0; k < arrayOfChar.length; k++)
	    {
	      char c = arrayOfChar[k];
	      switch (i)
	      {
	      case 0:
	        if (c == '<')
	          i = 1;
	        else
	          localStringBuffer.append(c);
	        break;
	      case 1:
	        if (c == '>')
	        {
	          i = 0;
	        }
	        else if (c == '"')
	        {
	          j = i;
	          i = 5;
	        }
	        else if (c == '\'')
	        {
	          j = i;
	          i = 4;
	        }
	        else if (c == '/')
	        {
	          i = 3;
	        }
	        else
	        {
	          if ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r') || (c == '\f'))
	            continue;
	          i = 2;
	        }
	        break;
	      case 2:
	      case 3:
	        if (c == '>')
	        {
	          i = 0;
	        }
	        else if (c == '"')
	        {
	          j = i;
	          i = 5;
	        }
	        else if (c == '\'')
	        {
	          j = i;
	          i = 4;
	        }
	        else if (c == '"')
	        {
	          i = 5;
	        }
	        else
	        {
	          if (c != '\'')
	            continue;
	          i = 4;
	        }
	        break;
	      case 5:
	        if (c != '"')
	          continue;
	        i = j;
	        break;
	      case 4:
	        if (c != '\'')
	          continue;
	        i = j;
	      }
	    }
	    return localStringBuffer.toString();
	  }

	  public static String applyFilter(String paramString)
	  {
	    try
	    {
	      for (paramString = Pattern.compile("<([^>]*) on([A-Z]+)[\t\n\r ]*=([^>]*)>", 2).matcher(paramString).replaceAll("<$1 no-on$2=$3>"); Pattern.compile("<([^>]*)(?:/\\*(?:.|\\s)*?\\*/)([^>]*)>", 2).matcher(paramString).find(); paramString = Pattern.compile("<([^>]*)(?:/\\*(?:.|\\s)*?\\*/)([^>]*)>", 2).matcher(paramString).replaceAll("<$1$2>"));
	      paramString = Pattern.compile("<([^>]*) style[\t\n\r ]*=['\"xss: ]*expression['\" ]*([^>]*)>", 2).matcher(paramString).replaceAll("<$1>");
	      paramString = Pattern.compile("<[\t\n\r ]*/?[\t\n\r ]*SCRIPT[\t\n\r ]*[^>]*>", 2).matcher(paramString).replaceAll("<!--<script>-->");
	      paramString = Pattern.compile("<[^>]+((j|&#(106|x6A)[;]*)[\t\n\r]*(a|&#(97|x61)[;]*)[\t\n\r ]*(v|&#(118|x76)[;]*)[\t\n\r]*(a|&#(97|x61)[;]*)|v[\t\n\r]*b)[\t\n\r]*(s|&#(115|x73)[;]*)[\t\n\r]*(c|&#(99|x63)[;]*)[\t\n\r]*(r|&#(114|x72)[;]*)[\t\n\r]*(i|&#(105|x69)[;]*)[\t\n\r]*(p|&#(112|x70)[;]*)[\t\n\r]*(t|&#(116|x74)[;]*)[\t\n\r]*(:|&#(58|x3A)[;]*)[^>]+>", 2).matcher(paramString).replaceAll("<!--<javascript>-->");
	      paramString = Pattern.compile("<[\t\n\r ]*META[^>]*>", 2).matcher(paramString).replaceAll("<!--<meta>-->");
	      paramString = Pattern.compile("<[\t\n\r ]*/?[\t\n\r ]*IFRAME[^>]*>", 2).matcher(paramString).replaceAll("<!--<iframe>-->");
	      paramString = Pattern.compile("<[\t\n\r ]*LINK[^>]*>", 2).matcher(paramString).replaceAll("<!--<link>-->");
	      paramString = Pattern.compile("<[\t\n\r ]*/?[\t\n\r ]*FORM[^>]*>", 2).matcher(paramString).replaceAll("<!--<form>-->");
	      paramString = Pattern.compile("<([^>]*) TYPE[\t\n\r ]*=['\" ]*text/x-scriptlet['\" ]*([^>]*)>", 2).matcher(paramString).replaceAll("<$1 TYPE=\"text/plain\" $2>");
	      paramString = Pattern.compile("<([^>]*) name[\t\n\r ]*=['\" ]*AllowScriptAccess['\" ]* value[\t\n\r ]*=['\" ]*always['\" ]*([^>]*)>", 2).matcher(paramString).replaceAll("<$1 name=\"allowScriptAccess\" value=\"never\" $2>");
	      paramString = Pattern.compile("<([^>]*) AllowScriptAccess[\t\n\r ]*=['\" ]*always['\" ]*([^>]*)>", 2).matcher(paramString).replaceAll("<$1 allowScriptAccess=\"never\" $2>");
	    }
	    catch (Exception localException)
	    {
	    }
	    return paramString;
	  }
	}
